package com.vca.admin.aop;

import cn.hutool.core.util.ObjectUtil;
import com.vca.admin.filter.TokenComponent;
import com.vca.common.vo.LoginUserVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class RefushToken {
    public static final Logger logger = LoggerFactory.getLogger(RefushToken.class);

    @Resource
    private TokenComponent tokenComponent;


    @Pointcut("execution(* com.vca.admin.controller.*.*(..))")
    public void pointCutMethodController() {

    }

    @Around("pointCutMethodController()")
    public Object doAroundService(ProceedingJoinPoint pjp) throws Throwable{
        long begin = System.nanoTime();
        logger.info("——————————————————————刷新token—————————Start—————————————");
        // 获得request对象,response对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        LoginUserVo loginUser = tokenComponent.getLoginUser(request);
        if (ObjectUtil.isNotNull(loginUser)){
            tokenComponent.refreshToken(loginUser);
        }
        Object obj = pjp.proceed();

        long end = System.nanoTime();

        logger.info("Controller method：{}，prams：{}，cost time：{} ns，cost：{} ms",

                pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()), (end - begin), (end - begin) / 1000000);

        logger.info("——————————————————————刷新token————————End——————————————");
        return obj;
    }
}
